import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
/**
 * Generated class for the Resourceswaitjod page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-resourceswaitjod',
	templateUrl: 'resourceswaitjod.html',
})
export class ResourceswaitjodPage {
	private listItemsPage: any;

	/**
 * 列表
 */
	private listItems: Array<{
		id: any,
		assetCode: any,
		assetName: any,
		specFormula: any,
		numbers: any,
		originalValue: any,
		manageDept: any,
		assetBelong: any,
		updateDate: any
	}>;

	/**
	 * 选择框
	 */
	private inputSearch = {
		title: ''
	};

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider) {
		this.listItems = new Array();
		this.listItemsPage = 1;
	}


	ionViewDidLoad() {
		console.log('ionViewDidLoad Resourcesnowjod');
		this.loadData();
	}

	loadData() {
		//异步方式
		return new Promise((success) => {
			this.storage.getStorage("user").then(val => {
			//初始化
			this.listItemsPage = 1;
				//请求方式如下
				let params = {
					user_id: val['user_id'],
					pageNo: this.listItemsPage,
					name: this.inputSearch.title
				};
				//收文传阅列表
				this.http.postJson('oa', 'doneList.do', params).then(res => {
					if (res['data'] != undefined) {
					let datas = res['data']['list'];
					this.loadMakeData(true, datas);
					if (success)
						success();
					}else{
						if (success)
							success();
					}
				});
			});
		});
	}
	//加载更多
	loadMoreData() {
		//异步方式
		return new Promise((success) => {
this.storage.getStorage("user").then(val => {
				//请求方式如下
				let params = {
					user_id: val['user_id'],
					pageNo: this.listItemsPage + 1,
					name: this.inputSearch.title
				};
				//收文传阅列表
				this.http.postJson('oa', 'doneList.do', params).then(res => {
					if (res['data'] != undefined) {
					let datas = res['data']['list'];
					this.loadMakeData(false, datas);
					if (datas.length > 0)
						this.listItemsPage++;
					if (success)
						success();
					}else{
						if (success)
							success();
					}
				});
			});
		});
	}
	//共通列表
	loadMakeData(isnew: any, datas: any) {
		if (isnew)
			this.listItems = new Array();
		for (let i = 0; i < datas.length; i++) {
			let item = datas[i];
			this.listItems.push({
				id: item['id'],
				assetCode: item['assetCode'],
				assetName: item['assetName'],
				specFormula: item['specFormula'],
				numbers: item['numbers'],
				originalValue: item['originalValue'],
				manageDept: item['manageDept'],
				assetBelong: item['assetBelong'],
				updateDate: item['updateDate'],
			});
		}
	}
	//搜索
	dosearch() {
		let data = new Object();
		let params = new Object();
		params['applyapplytruename'] = this.inputSearch.title;
		data['type'] = 8; //检索类型
		data['params'] = params; //参数传递 
		let opts = new Object();
		this.dialog.showModal("ModalsearchPage", data, opts).then(success => {
			//界面关闭的时候
			if (success) {
				this.inputSearch.title = success['applyapplytruename'];
				this.loadData();
			}
		});
	}

	/*下拉加载*/
	doRefresh(refresher) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			/*判断是哪个列表显示*/
			this.loadData().then(success => {
				refresher.complete();
			});
		}, 1500);
	}
	/*上拉加载*/
	doInfinite(infiniteScroll) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			this.loadMoreData().then(success => {
				infiniteScroll.complete();
			});
		}, 1500);
	}
	//详情
	dodetail(item: any) {

	}

}
